package com.edu.zut.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

/**
 * @Author 许文科
 * @Date 2023/4/19 21:45
 * @Description paperdetect-bcak
 */
public class TxtToPDF {
    public String tranfer(String txtPath, String name) throws IOException, DocumentException {
        String txtContent = "";
        //前缀
//        String prefix = FileUtil.getPrefix(txtPath);
//        System.out.println("prefix = " + prefix);
        String prefix = txtPath + "//" + name + ".txt";
        String result = txtPath + "//" + name;

        BufferedReader br = new BufferedReader(new FileReader(prefix));
        String line;
        while ((line = br.readLine()) != null) {
            txtContent += line + "\n";
        }
        br.close();
        //使用Document将内容转换为PDF
        Document document = new Document();
        //创建PdfWriter
        PdfWriter.getInstance(document, new FileOutputStream(result + ".pdf"));
        //打开Document
        document.open();
        //添加内容
        document.add(new Paragraph(txtContent));
        //关闭document
        document.close();
        return result + ".pdf";


    }

}
